<?php

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Auth.php';

class Safe_RegistrationController extends Zend_Controller_Action
{
    public function init()
    {
        parent::init();
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$this->_redirect('/');
		}
		$this->_root = dirname(dirname(__FILE__));
	}
	
	public function indexAction()
	{
		$this->view->title = 'Registration';
        $this->view->message = '';
		$form = $this->getForm();
		$this->view->form = $form;
	}

    public function submitAction()
    {
		$this->view->title = 'Registration';
        $this->view->message = '';
		$request = $this->getRequest();
		$form = $this->getForm();
		$this->view->form = $form;
		if($request->isPost()) {
			if($form->isValid($request->getPost())) {
				$formResult = $form->getValues();
				$alias	= $formResult['alias'];
				$email	= $formResult['email'];
				$pass 	= $this->randPass();				
				//$dbMySQL = Zend_Registry::get('mysql');
				
                $credential($this->randPass());
				
            }
        }
		
		//return $this->render('index'); 
	}


	public function randPass($char=5) {
		
		$chars = "ACDEFGHKLMNPQRTZ34679";
		srand(time());
		
		$i = 0;
		$pass = '' ;
		
		while ($i <= $char) {
			$num = rand() % 33;
			$tmp = substr($chars, $num, 1);
			$pass = $pass . $tmp;
			$i = strlen($pass);
		}
		return $pass;
	}
	
	private function getForm()
	{
		require_once $this->_root.'/forms/RegistrationForm.php';
		return new RegistrationForm(array(
		'action' => '/safe/registration/submit',
		'method' => 'post',
		));
	}

}
?>
